package com.kitchen.com
{
	import com.kitchen.KitchenEvent;
	import com.kitchen.ui.button.ScrollButton;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class VerticalScrollBar extends Sprite
	{
		public static const E_SCROLL:String = 'scroll';
		
		public var h:Number;
		public var w:Number;
		private var sbtn:ScrollButton;
		private var bitdata:BitmapData;
		private var state:int = 0;
		public var rate:Number;
		
		public function VerticalScrollBar(height:Number, bitdata:BitmapData)
		{
			super();
			this.h = height;
			this.state = 0;
			this.bitdata = bitdata;
			this.layout();
		}
		
		private function layout():void{
			this.sbtn = new ScrollButton(this.bitdata);
			this.w = this.sbtn.w;
			this.graphics.lineStyle(2, 0x8b7e76);
			this.graphics.moveTo(this.w/2, -this.sbtn.h/2+5);
			this.graphics.lineTo(this.w/2, this.h+this.sbtn.h/2-5);
			this.graphics.endFill();
			this.sbtn.x = 0;
			this.sbtn.y = 0;
			this.sbtn.addEventListener(MouseEvent.MOUSE_DOWN, downHandle);
			this.sbtn.addEventListener(MouseEvent.MOUSE_MOVE, moveHandle);
			this.sbtn.addEventListener(MouseEvent.MOUSE_UP, upHandle);
			this.sbtn.addEventListener(MouseEvent.MOUSE_OUT, upHandle);
			this.addChild(this.sbtn);
		}
		
		private function downHandle(me:MouseEvent):void{
			this.state = 1;
		}
		
		private function moveHandle(me:MouseEvent):void{
			if(this.state == 1){
				this.sbtn.startDrag(false, new Rectangle(0, 0, 1, this.h));
				this.rate = Math.ceil(this.sbtn.y*1000/this.h)/1000;
				this.dispatchEvent(new KitchenEvent(KitchenEvent.SCROLL));
			}
		}
		
		private function upHandle(me:MouseEvent):void{
			this.state = 0;
			this.sbtn.stopDrag();
		}
		
	}
}